package edu.mtu.citizenscience.cs.sdk;

import java.util.Map;

import android.util.Log;

import com.androidquery.AQuery;

import edu.mtu.citizenscience.cs.sdk.json.Request;
import edu.mtu.citizenscience.cs.sdk.login.CitsciLogin;
import edu.mtu.citizenscience.cs.sdk.login.UserLogin;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Configures and executes requests to the CitSci Server
 */
public class ServerRequest {
	// the URL to send requests to
	//public static final String API_URL = "http://citizenscience.cs.mtu.edu/api";
	public static final String API_URL = "http://citizenscience.cs.mtu.edu/test/api";
	//public static final String API_URL = "http://10.0.1.2:10088/citsci/www/api";
	
	// the version of the API to use
	public static String API_VERSION = "v1";
	
	// holds the API key for the app using the SDK
	public static String API_KEY;
	// holds the upload type for app using the SDK
	public static String UPLOAD_TYPE;
	
	/**
	 * Configure the app server connection
	 * 
	 * @param   uploadType - String - The type of upload such as mushroom
	 * @param	apiKey - String - the api key for the app
	 * @return	void
	 */
	public static void configureServer(String uploadType, String apiKey)
	{
		API_KEY = apiKey;
		UPLOAD_TYPE = uploadType;
	}
	
	/**
	 * Configure the app server connection
	 * 
	 * @param	version - String - the API version to interact with
	 * @param   uploadType - String - the type of upload such as mushroom
	 * @param	apiKey - String - the api key for the app
	 * @return	void
	 */
	public static void configureServer(String version, String uploadType, String apiKey)
	{
		configureServer(uploadType, apiKey);
		
		API_VERSION = version;
	}
	
	/**
	 * Do a request on the CitSci server
	 * 
	 * @param	aq - AQuery - the AQuery instance to make the request with
	 * @param	resource - String - the resource to make the request against
	 * @param	request - Request<?> - the request to make from the server
	 * @param	handler - ResponseHandler<?> - the response handler for this specific request
	 * @return	void
	 */
	public static void doRequest(AQuery aq, String resource, Request<?> request, ResponseHandler<?> handler)
	{	
		// get the params from the request object
		Map<String, Object> params = request.getParams();
		
		// get the users login from shared preferences
		UserLogin login = new CitsciLogin(aq).getLogin();
		
		// check if got stored login
		if (login.haveLogin())
		{
			// add the username and key to the params being sent in the request
			params.put("username", login.getUsername());
			params.put("remoteKey", login.getKey());
		}
		
		// build the request url from the API url + API version + the resource
		String url = ServerRequest.API_URL + "/" + ServerRequest.API_VERSION + "/" + resource + "/";
		
		// check if the request contains a resource id
		if (request.getId() > -1)
		{
			// add the id to the request url
			url += request.getId() + "/";
		}
		
		// add the request action and format to the url
		url += request.getAction() + "." + request.getFormat(); 
		
		Log.d("SDK", "New request:" + url);
		Log.d("SDK", "Params:");
		for (Map.Entry<String, Object> entry : params.entrySet())
		{
			Log.d("SDK", "Name: " + entry.getKey());
			if (entry.getValue() != null)
			{
				Log.d("SDK", "Value: " + entry.getValue().toString());
			}
		}
		
		// make the request to the server
		aq.ajax(url, params, String.class, new RequestCallback(handler));
	}
	
}